<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="welcome-text">
          <h2>欢迎使用</h2>
          <h1>教师考勤系统</h1>
          <p>便捷、高效的课堂考勤解决方案</p>
        </div>
      </div>
      <div class="login-right">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left">
          <div class="title-container">
            <h3 class="title">账号登录</h3>
            <p class="subtitle">请使用教师账号登录系统</p>
          </div>

          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              name="username"
              type="text"
              auto-complete="on">
              <i slot="prefix" class="el-icon-user"></i>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              name="password"
              :type="passwordType"
              auto-complete="on">
              <i slot="prefix" class="el-icon-lock"></i>
              <i slot="suffix"
                class="el-icon-view password-eye"
                @click="showPassword"
                :class="{ 'is-active': passwordType === '' }">
              </i>
            </el-input>
          </el-form-item>

          <el-button
            :loading="loading"
            type="primary"
            class="login-button"
            @click.native.prevent="handleLogin">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>

          <div class="tips">
            <p>推荐使用Chrome浏览器访问系统</p>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      passwordType: 'password',
      loading: false
    }
  },
  methods: {
    showPassword() {
      this.passwordType = this.passwordType === 'password' ? '' : 'password'
    },
    async handleLogin() {
      try {
        const response = await this.$http.post('/api/user/login', this.loginForm)
        console.log('登录响应:', response)

        if (response.code === 1) {
          const userData = response.data
          console.log('用户数据:', userData)

          // 检查用户角色
          if (userData.role !== 1) {
            this.$message.error('非教师账号无法登录教师端')
            return
          }

          // 存储用户信息
          const userInfo = {
            name: userData.name,
            school: userData.school,
            department: userData.department,
            profession: userData.profession
          }
          localStorage.setItem('userInfo', JSON.stringify(userInfo))
          localStorage.setItem('token', userData.token)

          // 设置请求头
          this.$http.defaults.headers.common['Authorization'] = userData.token

          this.$message.success('登录成功')
          this.$router.push('/home')
        } else {
          this.$message.error(response.msg || '登录失败')
        }
      } catch (error) {
        console.error('登录失败:', error)
        this.$message.error('登录失败')
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  background: linear-gradient(135deg, #1e80ff 0%, #1e80ff 100%);
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 1000px;
  height: 600px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  display: flex;
  overflow: hidden;
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #1e80ff 0%, #1e80ff 100%);
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
}

.welcome-text {
  text-align: center;
}

.welcome-text h1 {
  font-size: 36px;
  margin: 20px 0;
  font-weight: bold;
}

.welcome-text h2 {
  font-size: 24px;
  margin-bottom: 10px;
}

.welcome-text p {
  font-size: 16px;
  opacity: 0.8;
}

.login-right {
  flex: 1;
  background: white;
  padding: 40px;
  display: flex;
  align-items: center;
}

.login-form {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.title-container {
  text-align: center;
  margin-bottom: 40px;
}

.title {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
  font-weight: bold;
}

.subtitle {
  color: #666;
  font-size: 14px;
}

.el-input {
  height: 50px;
}

.el-input input {
  height: 50px;
  background: #f5f7fa;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  color: #333;
  padding-left: 45px;
  transition: all 0.3s;
}

.el-input input:focus {
  border-color: #1e80ff;
  background: white;
}

.el-form-item {
  margin-bottom: 25px;
  border: none;
  background: none;
}

.el-input i[class^="el-icon"] {
  color: #909399;
  font-size: 18px;
  margin: 0 10px;
  transition: all 0.3s;
}

.password-eye {
  cursor: pointer;
}

.password-eye.is-active {
  color: #1e80ff !important;
}

.login-button {
  width: 100%;
  height: 50px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: bold;
  margin-top: 10px;
  background: #1e80ff;
  border-color: #1e80ff;
  transition: all 0.3s;
}

.login-button:hover {
  background: #0060e6;
  border-color: #0060e6;
  transform: translateY(-2px);
  box-shadow: 0 8px 15px rgba(30, 128, 255, 0.3);
}

.tips {
  text-align: center;
  margin-top: 20px;
  color: #909399;
  font-size: 13px;
}

@media screen and (max-width: 1200px) {
  .login-box {
    width: 90%;
    max-width: 1000px;
  }
}

@media screen and (max-width: 768px) {
  .login-box {
    flex-direction: column;
    height: auto;
  }

  .login-left {
    padding: 30px;
  }

  .login-right {
    padding: 30px;
  }

  .welcome-text h1 {
    font-size: 28px;
  }

  .welcome-text h2 {
    font-size: 20px;
  }
}
</style>
